﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindLinkForum
{
    public class DataProvider
    {
        private static string _connectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;


        public static SqlConnection _connection;
        public static SqlCommand _command;
        public static SqlDataAdapter _adapter;
        public static SqlParameter _para;


        public static void Connnect()
        {
            _connection = new SqlConnection(_connectionString);
            _connection.Open();
        }

        public static void Disconect()
        {
            if (_connection.State == ConnectionState.Connecting)
            {
                _connection.Close();
            }
        }

        // ExcuteQuery
        public static DataTable ExcuteQuery(string sql)
        {
            _command = new SqlCommand(sql, _connection);
            _adapter = new SqlDataAdapter();
            _adapter.SelectCommand = _command;
            DataTable dt = new DataTable();
            _adapter.Fill(dt);
            return dt;
        }

        // ExcuteNonQuery
        public static int ExcuteNonQuery(string sql)
        {
            _command = new SqlCommand(sql, _connection);
            return _command.ExecuteNonQuery();
        }

        // ExcuteScalar
        public static object ExcuteScalar(string sql)
        {
            _command = new SqlCommand(sql, _connection);
            return _command.ExecuteScalar();
        }
    }
}
